package com.glory.good_dark.activity;

import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.good_dark.R;
import com.glory.good_dark.adapter.StripIDListAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.SystemConfigEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import butterknife.BindView;

public class StripIDManger2Activity extends BaseProjectActivity {

    @BindView(R.id.rvStripInfoList)
    RecyclerView rvStripInfoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private StripIDListAdapter mAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_str_ip_i_d_manger;
    }

    @Override
    protected void initView() {
        initRecyclerView(rvStripInfoList,this);
        mAdapter = new StripIDListAdapter(null, getSupportFragmentManager(), new StripIDListAdapter.AddAndDeleteCallback() {
            @Override
            public void onAddCallback(SystemConfigEntity.SystemType item) {
                FreeUI_EntityCallBack<BaseEntity<SystemConfigEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<SystemConfigEntity>>
                        (new TypeToken<BaseEntity<SystemConfigEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess
                            (final Response<BaseEntity<SystemConfigEntity>> response) {
                        if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                            showToast("添加成功",true);
                            initData();
                        }
                    }

                    @Override
                    public void onError
                            (Response<BaseEntity<SystemConfigEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "PDAAddStripID");
                    }
                };
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("EQP_ID", App.EQPID);
                jsonObject.addProperty("OPERATOR", App.userEntity.getWORKNO());
                jsonObject.addProperty("Lot_id", item.getLOT_ID());
                jsonObject.addProperty("StripID", item.getStripID());
                NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAAddStripID", jsonObject);
            }

            @Override
            public void onDeleteCallback(SystemConfigEntity.SystemType item, String stripID) {
                FreeUI_EntityCallBack<BaseEntity<SystemConfigEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<SystemConfigEntity>>
                        (new TypeToken<BaseEntity<SystemConfigEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess
                            (final Response<BaseEntity<SystemConfigEntity>> response) {
                        if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                            showToast("删除成功",true);
                            initData();
                        }
                    }

                    @Override
                    public void onError
                            (Response<BaseEntity<SystemConfigEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "PDADeleteStripID");
                    }
                };
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("EQP_ID", App.EQPID);
                jsonObject.addProperty("OPERATOR", App.userEntity.getWORKNO());
                jsonObject.addProperty("Lot_id", item.getLOT_ID());
                jsonObject.addProperty("StripID", stripID);
                NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDADeleteStripID", jsonObject);
            }
        });
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mAdapter.isFirstOnly(false);
        rvStripInfoList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        FreeUI_EntityCallBack<BaseEntity<SystemConfigEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<SystemConfigEntity>>
                (new TypeToken<BaseEntity<SystemConfigEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<SystemConfigEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    SystemConfigEntity body = response.body().getData().getBody();
                    mAdapter.setNewData(body.getLotInfo());
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<SystemConfigEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAGetStripIdInfo");
            }
        };
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        jsonObject.addProperty("OPERATOR", App.userEntity.getWORKNO());
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAGetStripIdInfo", jsonObject);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("StripId管理");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FreeUI_EntityCallBack<BaseEntity<SystemConfigEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<SystemConfigEntity>>
                        (new TypeToken<BaseEntity<SystemConfigEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess
                            (final Response<BaseEntity<SystemConfigEntity>> response) {
                        if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                            showToast("提交成功",true);
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    StripIDManger2Activity.this.finish();
                                }
                            },2500);
                        }
                    }

                    @Override
                    public void onError
                            (Response<BaseEntity<SystemConfigEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "PDASubmitStripIdinfo");
                    }
                };
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("EQP_ID", App.EQPID);
                jsonObject.addProperty("OPERATOR", App.userEntity.getWORKNO());
                jsonObject.add("LotInfo",new JsonParser().parse(new Gson().toJson(mAdapter.getData())).getAsJsonArray());
                NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDASubmitStripIdinfo", jsonObject);
            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {

    }
}